import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    EditDtoExtensionsItem,
    StockZT_RoomServiceProxy,
    StockZT_LocationServiceProxy,
    StockZT_IntoBillItemServiceProxy,
    Stock_IntoMarketItemEditDto
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';

interface SelectedItems {
    name: string;
    code: string;
    id: string;
}
@Component({
    selector: 'marketIntoBillItemDetailModal',
    templateUrl: './market-into-bill-item-detail-modal.component.html'
})
export class MarketIntoBillItemDetailModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _billService: StockZT_IntoBillItemServiceProxy,
    ) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;

    editDto: Stock_IntoMarketItemEditDto;
    extensions: Array<EditDtoExtensionsItem>;
    roomOption: Array<object> = [];
    f_StorageAreaCodeOption;
    optionAll;
    f_Warehouse: SelectedItems;
    f_StorageArea: SelectedItems;
    ngOnInit() {}

    show(id?: string): void {
        this.active = true;
        this.f_Warehouse = null;
        this.f_StorageArea = null;

        this._billService.getIntoMarketItemForEditExtension(id).subscribe(result => {
            let optionAll = {};

            if (result.selects) {
                result.selects.forEach(el => {
                    optionAll[el.label] = el.value;
                });
            }
            this.optionAll = optionAll;
            this.editDto = result.dto;
            this.extensions = result.items;
            this.extensions.forEach(el => {
                el.readonly = true;
            });
            this.modal.show();
        });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
